<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="../kindeditor/themes/default/default.css" />
<script charset="utf-8" src="../kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="../kindeditor/lang/zh_CN.js"></script>
<script type="text/javascript">
KindEditor.ready(function(K){
K.create('textarea[name="description"]',{
uploadJson : '../upload_json.php',
fileManagerJson : '../file_manager_json.php',
allowFileManager: true,
});
K.create('textarea[name="input"]',{
uploadJson : '../upload_json.php',
fileManagerJson : '../file_manager_json.php',
allowFileManager: true,
});
K.create('textarea[name="output"]',{
uploadJson : '../upload_json.php',
fileManagerJson : '../file_manager_json.php',
allowFileManager: true,
});
K.create('textarea[name="hint"]',{
uploadJson : '../upload_json.php',
fileManagerJson : '../file_manager_json.php',
allowFileManager: true,
});
});

</script>
<title>编辑问题</title>
</head>
<body>

<?php require_once("../include/db_info.inc.php");?>
<?php require_once("admin-header.php");

if (!(isset($_SESSION['administrator'])
      ||isset($_SESSION['problem_editor'])
     )){
	echo "<a href='../loginpage.php'>Please Login First!</a>";
	exit(1);
}
?>

<?php if(isset($_GET['id'])){
	require_once("../include/check_get_key.php");
?>
<h1>编辑问题</h1>

<form class="form-horizontal col-sm-6 col-sm-offset-2" action="problem_edit.php" method="post">

<?php $sql="SELECT * FROM `problem` WHERE `problem_id`=".intval($_GET['id']);
$result=mysql_query($sql);
$row=mysql_fetch_object($result);
?>

<div class="form-group">
        <label class="col-sm-5 control-label">问题编号</label>
        <div class="col-sm-7">		    
		<input class="form-control" id="disabledInput" type="text" value='<?php echo $row->problem_id?>' disabled></div>
<input type=hidden name=problem_id value='<?php echo $row->problem_id?>'>		
</div>

<div class="form-group">
        <label class="col-sm-5 control-label">标题</label>
        <div class="col-sm-7">
        <input class="form-control" name="title" type="text" value='<?php echo htmlspecialchars($row->title)?>'/></div>
</div>
<div class="form-group">
        <label class="col-sm-5 control-label">时间限制</label>
        <div class="col-sm-7">
        <input class="form-control" name="time_limit" type="text" value='<?php echo $row->time_limit?>'/></div>
</div>
<div class="form-group">
        <label class="col-sm-5 control-label">内存限制</label>
        <div class="col-sm-7">
        <input class="form-control" name="memory_limit" type="text" value='<?php echo $row->memory_limit?>'/></div>
</div>
<div class="form-group">
        <label class="col-sm-5 control-label">题目难度</label>
        <div class="col-sm-7">
        <input class="form-control" name="problem_hard" type="text" value='<?php echo $row->hard?>'/></div>
</div>



<p align=left>Description:<br><textarea rows=13 name=description cols=80><?php echo $row->description;?></textarea></p>
<p align=left>Input:<br><textarea rows=13 name=input cols=80><?php echo $row->input;?></textarea></p>
<p align=left>Output:<br><textarea rows=13 name=output cols=80><?php echo $row->output;?></textarea></p>

<p>Sample Input:<br><textarea rows=6 name=sample_input cols=80><?php echo htmlspecialchars($row->sample_input)?></textarea></p>
<p>Sample Output:<br><textarea rows=6 name=sample_output cols=80><?php echo htmlspecialchars($row->sample_output)?></textarea></p>

<p align=left>Hint:<br><textarea rows=13 name=hint cols=80><?php echo $row->hint;?></textarea></p>

<p>SpecialJudge: 
N<input type=radio name=spj value='0' <?php echo $row->spj=="0"?"checked":""?>>
Y<input type=radio name=spj value='1' <?php echo $row->spj=="1"?"checked":""?>></p>

<div class="form-group">
	<label class="col-sm-5 control-label">题库</label>
	<div class="col-sm-7">
	<select class="form-control" name="class">
       <option value="0" <?php if(($row->class)==0)echo "selected";?>>主题库</option>
       <option value="1" <?php if(($row->class)==1)echo "selected";?>>Begin题库</option>
       <option value="2" <?php if(($row->class)==2)echo "selected";?>>比赛题库</option>
       
    </select>
	</div>
</div>

<div align=center>
<?php require_once("../include/set_post_key.php");?>

<div class="form-group">
        <div class="col-sm-offset-5 col-sm-7">
            <input type="submit" class="btn btn-primary col-sm-8" value="提交"/>
        </div>
</div>

</div></form>




<?php }else{
require_once("../include/check_post_key.php");
$id=intval($_POST['problem_id']);
if(!(isset($_SESSION["p$id"])||isset($_SESSION['administrator']))) exit();	
$title=$_POST['title'];
$time_limit=$_POST['time_limit'];
$memory_limit=$_POST['memory_limit'];
$problem_hard=$_POST['problem_hard'];
$description=$_POST['description'];
$input=$_POST['input'];
$output=$_POST['output'];
$sample_input=$_POST['sample_input'];
$sample_output=$_POST['sample_output'];
$hint=$_POST['hint'];
$source=$_POST['source'];
$spj=$_POST['spj'];
$class=$_POST['class'];

if (get_magic_quotes_gpc ()) {
	$title = stripslashes ( $title);
	$time_limit = stripslashes ( $time_limit);
	$memory_limit = stripslashes ( $memory_limit);
	$problem_hard = stripslashes ( $problem_hard);
	$description = stripslashes ( $description);
	$input = stripslashes ( $input);
	$output = stripslashes ( $output);
	$sample_input = stripslashes ( $sample_input);
	$sample_output = stripslashes ( $sample_output);
//	$test_input = stripslashes ( $test_input);
//	$test_output = stripslashes ( $test_output);
	$hint = stripslashes ( $hint);
	$source = stripslashes ( $source); 
	$spj = stripslashes ( $spj);
	$source = stripslashes ( $source );
}
$basedir=$OJ_DATA."/$id";
echo "<div class='alert alert-warning'> 点击<a href='problem_list.php'>返回</a> admin题目列表</div>";

	/*if($sample_input){
		//mkdir($basedir);
		$fp=fopen($basedir."/sample.in","w");
		fputs($fp,preg_replace("(\r\n)","\n",$sample_input));
		fclose($fp);

		$fp=fopen($basedir."/sample.out","w");
		fputs($fp,preg_replace("(\r\n)","\n",$sample_output));
		fclose($fp);
	}*/
	$title=mysql_real_escape_string($title);
	$time_limit=mysql_real_escape_string($time_limit);
	$memory_limit=mysql_real_escape_string($memory_limit);
	$problem_hard = mysql_real_escape_string($problem_hard);
	$description=mysql_real_escape_string($description);
	$input=mysql_real_escape_string($input);
	$output=mysql_real_escape_string($output);
	$sample_input=mysql_real_escape_string($sample_input);
	$sample_output=mysql_real_escape_string($sample_output);
//	$test_input=($test_input);
//	$test_output=($test_output);
	$hint=mysql_real_escape_string($hint);
	$source=mysql_real_escape_string($source);
//	$spj=($spj);

$sql="UPDATE `problem` set `title`='$title',`time_limit`='$time_limit',`memory_limit`='$memory_limit',`hard`='$problem_hard',
	`description`='$description',`input`='$input',`output`='$output',`sample_input`='$sample_input',`sample_output`='$sample_output',`hint`='$hint',`source`='$source',`spj`=$spj,`in_date`=NOW(),`class`=$class
	WHERE `problem_id`=$id";

@mysql_query($sql) or die(mysql_error());
echo "<div class='alert alert-warning'>修改成功！<a href='../problem.php?id=$id'>点击查看该题目!</a></div>";
}
?>
</body>
</html>